package com.eenie.common.ui;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.eenie.common.R;
import com.eenie.common.base.BaseActivity;
import com.eenie.common.base.WinMode;
import com.github.chrisbanes.photoview.OnViewTapListener;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;
import java.util.List;




public class PicPreviewActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    ViewPager mViewPager;
    TextView mTvCount;
    PhotoPageAdapter mPhotoPageAdapter;


    ArrayList<String> mImgUrls = new ArrayList<>();

    public static void start(Context context,String imgUrl) {
        ArrayList<String> url = new ArrayList<>();
        url.add(imgUrl);
        PicPreviewActivity.start(context, url);
    }

    public static void start(Context context,ArrayList<String> imgUrls) {

        context.startActivity(new Intent(context, PicPreviewActivity.class).putStringArrayListExtra("imgs", imgUrls));


    }


    @Override
    public int initContentView() {
        return R.layout.activity_pic_preview;
    }

    @Override
    public void initInjector() {
        setWinMode(WinMode.FULL);

        mImgUrls.addAll(getIntent().getStringArrayListExtra("imgs"));

    }

    @Override
    public void initUiAndListener(View view) {


        mViewPager = findViewById(R.id.page_pic);
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setAdapter(mPhotoPageAdapter = new PhotoPageAdapter(mContext, mImgUrls));
        mTvCount = findViewById(R.id.tv_count);

        mTvCount.setText(String.format("%s/%s", 1, mImgUrls.size()));

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mTvCount.setText(String.format("%s/%s", position + 1, mImgUrls.size()));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    private class PhotoPageAdapter extends PagerAdapter implements OnViewTapListener {
        List<String> urls = new ArrayList<>();
        LayoutInflater mLayoutInflater;
        Context mContext;

        public PhotoPageAdapter(Context context, List<String> urls) {
            this.urls.addAll(urls);
            mContext = context;
            mLayoutInflater = LayoutInflater.from(context);
        }


        @Override
        public int getCount() {
            return urls.size();
        }


        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view.equals(object);
        }


        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = mLayoutInflater.inflate(R.layout.sub_page_img_preview, container, false);
            final PhotoView imageView = view.findViewById(R.id.img_photo);
            imageView.setOnViewTapListener(this);
            String url = mImgUrls.get(position);
            Glide.with(mContext)
                    .load(url)
                    .into(imageView);
            container.addView(view, 0);
            return view;
        }


        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }


        @Override
        public void onViewTap(View view, float x, float y) {
            finish();
        }
    }


}

